package com.universal.schedule;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private EditText userName;
	private EditText userPassword;
	private Button mBtnLogin;
	private Button mBtnRegist;
	private SharedPreferences sp;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		sp = getSharedPreferences("account", Context.MODE_PRIVATE);
		findView();
		initData();
	}

	private void initData() {
		mBtnLogin.setOnClickListener(new OnClickListener() {

			private Editable text;

			@Override
			public void onClick(View v) {
				text = userName.getText();
				String name = userName.getText().toString();
				String password = userPassword.getText().toString();
				if((!name.equals("")&&!password.equals(""))){
					String spname = sp.getString("name"+name, "");
					String sppassword = sp.getString("password"+password, "");
					if(!spname.equals("")&&!sppassword.equals("")){
						Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
						Intent intent = new Intent(LoginActivity.this, MainActivity.class);
						startActivity(intent);
						finish();
					}else{
						userName.setText("");
						userPassword.setText("");
						Toast.makeText(LoginActivity.this, "帐号或密码失败,请重试", Toast.LENGTH_LONG).show();
					}
				}else{
					Toast.makeText(LoginActivity.this, "帐号或密码不能为空", Toast.LENGTH_LONG).show();
				}
			}
		});

		mBtnRegist.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
				startActivity(intent);
			}
		});

	}

	private void findView() {
		userName = (EditText) findViewById(R.id.user_phone_number_login);
		userPassword = (EditText) findViewById(R.id.user_password_login);
		mBtnLogin = (Button) findViewById(R.id.login);
		mBtnRegist = (Button) findViewById(R.id.login_to_register);

	}
}
